package com.zjsd.sqeybusiness.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.zjsd.sqeybusiness.dto.User;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.zjsd.sqeybusiness.domain.Payroll;
import com.zjsd.sqeybusiness.service.IPayrollService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 工资表Controller
 *
 * @author Zheng
 * @date 2021-09-28
 */
@RestController
@RequestMapping("/sqeybusiness/payroll")
public class PayrollController extends BaseController
{
    @Autowired
    private IPayrollService payrollService;

    @Autowired
    TokenService tokenService;

    /**
     * 查询工资表列表
     */
    @PreAuthorize("@ss.hasPermi('sqeybusiness:payroll:list')")
    @GetMapping("/list")
    public TableDataInfo list(Payroll payroll)
    {
        startPage();
        List<Payroll> list = payrollService.selectPayrollList(payroll);
        return getDataTable(list);
    }

    /**
     * 导出工资表列表
     */
    @PreAuthorize("@ss.hasPermi('sqeybusiness:payroll:export')")
    //@Log(title = "工资表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Payroll payroll)
    {
        List<Payroll> list = payrollService.selectPayrollList(payroll);
        ExcelUtil<Payroll> util = new ExcelUtil<Payroll>(Payroll.class);
        return util.exportExcel(list, "payroll");
    }

    /**
     * 获取工资表详细信息
     */
    @PreAuthorize("@ss.hasPermi('sqeybusiness:payroll:query')")
    @GetMapping(value = "/{payrollid}")
    public AjaxResult getInfo(@PathVariable("payrollid") Long payrollid)
    {
        return AjaxResult.success(payrollService.selectPayrollById(payrollid));
    }

    /**
     * 新增工资表
     */
    @PreAuthorize("@ss.hasPermi('sqeybusiness:payroll:add')")
    //@Log(title = "工资表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Payroll payroll)
    {
        return toAjax(payrollService.insertPayroll(payroll));
    }

    /**
     * 修改工资表
     */
    @PreAuthorize("@ss.hasPermi('sqeybusiness:payroll:edit')")
    //@Log(title = "工资表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Payroll payroll)
    {
        return toAjax(payrollService.updatePayroll(payroll));
    }

    /**
     * 删除工资表
     */
    @PreAuthorize("@ss.hasPermi('sqeybusiness:payroll:remove')")
    //@Log(title = "工资表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{payrollids}")
    public AjaxResult remove(@PathVariable Long[] payrollids)
    {
        return toAjax(payrollService.deletePayrollByIds(payrollids));
    }

    //@Log(title = "工资表导入", businessType = BusinessType.IMPORT)
    //  @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Payroll> util = new ExcelUtil<Payroll>(Payroll.class);
        List<Payroll> payrollList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = payrollService.importPayroll(payrollList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<Payroll> util = new ExcelUtil<Payroll>(Payroll.class);
        return util.importTemplateExcel("工资表模板");
    }


    //@Log(title = "下发工资表", businessType = BusinessType.UPDATE)
    @PostMapping("/{payrollids}")
    public AjaxResult send(@PathVariable Long[] payrollids)
    {
        return toAjax(payrollService.sendPayrollByIds(payrollids));
    }

    /*
        查询个人工资表
     */
    @PostMapping("/queryUserPayroll")
    public TableDataInfo queryUserPayroll(Payroll payroll)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser sysUser = loginUser.getUser();
        if(payroll.getUserid() == null){
            payroll.setUserid(sysUser.getUserId());
        }
        List<Payroll> payrollList = payrollService.queryUserPayroll(payroll);
        return getDataTable(payrollList);
    }

    @PostMapping("/confirmPayroll")
    public AjaxResult confirmPayroll(Payroll payroll)
    {
        int i = payrollService.confirmPayroll(payroll);
        return toAjax(i);
    }



}
